package com.itcast.springboot.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUploadUtils {
    private final static String localCommonPath = "C://Users//Administrator//Desktop//static//";
    private final static String serverCommonPath = "https://www.jingyuxianggan.com/";
    private final static String songPath = "music//";
    private final static String songPhotoPath = "images//";
    private final static String feedbackPhotoPath = "feedback//";

    //上传文件
    public static String fileUpload(Integer fileType, MultipartFile file) {
        //文件未上传
        if (file.isEmpty()) {
            return null;
        } else {
            //本地文件路径
            String localPath = null;
            //服务器文件路径
            String serverPath = null;
            //获取原始文件名称
            String fileName = file.getOriginalFilename();
            //取出文件的后缀
            int index = fileName.lastIndexOf(".");
            String suffix = fileName.substring(index + 1, fileName.length());
            //新的文件名字
            String newFileName = UUID.randomUUID().toString() + "." + suffix;
            System.out.println("新文件名称： " + newFileName);
            //图片在项目中的全路径（绝对路径）
//            String songFullPath = System.getProperty("user.dir").concat("\\src\\main\\resources\\" +
//                    "static\\songs\\" + newSongName);
//            System.out.println("在项目中的绝对路径： " + songFullPath);
//            String filePath = "D://songs//";

            //上传音乐本地路径
            if (fileType == 0) {
                localPath = localCommonPath.concat(songPath).concat(newFileName);
                serverPath = serverCommonPath.concat(songPath).concat(newFileName);
            } else if (fileType == 1) {
                //上传歌曲图片路径
                localPath = localCommonPath.concat(songPhotoPath).concat(newFileName);
                serverPath = serverCommonPath.concat(songPhotoPath).concat(newFileName);
            } else {
                //上传反馈图片
                localPath = localCommonPath.concat(feedbackPhotoPath).concat(newFileName);
                serverPath = serverCommonPath.concat(feedbackPhotoPath).concat(newFileName);
            }
            //上传路径：在内存中创建File文件映射对象
            File dest = new File(localPath);
            //父目录是否存在，不存在则创建
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                //创建文件
                file.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return serverPath;
        }
    }

    //删除文件
    public static Integer fileDelete(String fileLink) {
//        String filePath = "D://images//";
//        String photoName = "index.jpg";
//        String photoLocalPath = filePath.concat(photoName);
        String localFile = fileLink.replace(serverCommonPath, localCommonPath);
        System.out.println("删除文件：" + localFile);
        File file = new File(localFile);
        Integer resultInfo = null;
        if (file.exists()) {
            if (file.delete()) {
                resultInfo = 1;
            } else {
                resultInfo = -1;
            }
        } else {
            resultInfo = 0;
        }
        return resultInfo;
    }
}
